$(function() {
	departmentalCharge = 1;
	getDepartmentalCharge()
})

function getDepartmentalCharge() {
	getQSCostWord()
	const currYear = localStorage.getItem("year");
	const currMon = localStorage.getItem("month");
	$.get(
		`${API_URL}getdata?budgetYear=${currYear}&budgetMonth=${currMon}&status=2&chartPage=604&chartNo=2&username=${getUsername}`, {},
		function(res) {
			console.log(res)
			let re = eval("(" + res + ")");
			console.log(re)
			getQSCost(re.items[0].charts[0].data)
		}
	)

	$.get(
		`${API_URL}getdata?budgetYear=${currYear}&budgetMonth=${currMon}&status=2&chartPage=604&chartNo=3&username=${getUsername}`, {},
		function(res) {
			console.log(res)
			let re = eval("(" + res + ")");
			console.log(re)
			getDirect(re.items[0].charts[0].data)
		}
	)
	getTableData()

}

function getQSCostWord() {
	const currYear = localStorage.getItem("year");
	const currMon = localStorage.getItem("month");
	$.get(
		`${API_URL}getdata?budgetYear=${currYear}&budgetMonth=${currMon}&status=2&chartPage=604&chartNo=1&username=${getUsername}`, {},
		function(res) {
			console.log(res)
			let re = eval("(" + res + ")");
			console.log(re)
			let data = re.items[0].charts[0].data[0];
			$('.container .content .vessel .word').html(`<div class="">
									<p>本月发生总额</p>
									<h2>${FormatWYCash(data.key01)}<span>万元</span></h2>
									<div class="contrast">
										<div class="${data.key02>0?'rise':data.key02==0?'fair':'drop'}">
											<img src="${data.key02>0?'assets/images/departmental-charge/arrow-up-filling.svg':data.key02==0?'assets/images/departmental-charge/icon-pingjun@2x.png':'assets/images/departmental-charge/arrow-down-filling.svg'}" >
											<span>${(data.key02 * 100).toFixed(1)}%</span>
										</div>
										<p>较上月</p>
									</div>
								</div>
								<div class="">
									<p>本年累计总额</p>
									<h2>${FormatWYCash(data.key03)}<span>万元</span></h2>
									<div class="contrast">
										<div class="${data.key04>0?'rise':data.key04==0?'fair':'drop'}">
											<img src="${data.key04>0?'assets/images/departmental-charge/arrow-up-filling.svg':data.key04==0?'assets/images/departmental-charge/icon-pingjun@2x.png':'assets/images/departmental-charge/arrow-down-filling.svg'}" >
											<span>${(data.key04 * 100).toFixed(1)}%</span>
										</div>
										<p>较上年</p>
									</div>
								</div>`)
		}
	)

}

function getQSCost(data) {
	let benyueData = [];
	let bennianData = [];
	let legendData = ['本月发生', '本年累计'];
	let xData = [];
	for (let i = 0; i < data.length; i++) {
		xData.push(data[i].key01);
		benyueData.push(FormatWYCash(data[i].key02));
		bennianData.push(FormatWYCash(data[i].key03));
	}

	let seriesData = [{
			name: "本月发生",
			barWidth: "30%",
			type: "bar",
			data: benyueData,
			itemStyle: {
				color: "#20C997",
			},
			label: {
				show: true,
				position: 'top',
				fontSize: 12,
				color: "#1D2129",
				fontWeight: 600,
			},
		},
		{
			name: "本年累计",
			barWidth: "30%",
			type: "bar",
			data: bennianData,
			itemStyle: {
				color: "#165DFF",
			},
			label: {
				show: true,
				position: 'top',
				fontSize: 12,
				color: "#1D2129",
				fontWeight: 600,
			},
		}
	];
	have("qs-chart") ? monthTrend("qs-chart", seriesData, legendData, xData, 123) : "";
}


function getDirect(data) {
	let xData = [];
	let colorList = ['#165DFF', '#20C997', '#F7BA1E'];
	let yData = []
	let ydata1 = []
	let ydata2 = []
	let ydata3 = []
	let legendData = ['工资及福利费用', '研制费用', '管理费用'];
	let seriesData = []
	let xData1 = []
	for (let i = 0; i < data.length; i++) {
		xData.push(data[i].key01)
		ydata1.push(FormatWYCash(data[i].key02))
		ydata1.push(FormatWYCash(data[i].key05))
		ydata2.push(FormatWYCash(data[i].key03))
		ydata2.push(FormatWYCash(data[i].key06))
		ydata3.push(FormatWYCash(data[i].key04))
		ydata3.push(FormatWYCash(data[i].key07))
	}
	yData = [ydata1, ydata2, ydata3]
	console.log(yData)
	for (let i = 0; i < xData.length; i++) {
		xData1.push('本月')
		xData1.push('本年')
	}
	for (let i = 0; i < legendData.length; i++) {
		seriesData.push({
			name: legendData[i],
			type: 'bar',
			stack: 'a',
			barWidth: '60%',
			color: colorList[i],
			data: yData[i]
		})
	}
	have('direct-chart') ? getDirectDetail('direct-chart', legendData, seriesData, xData, xData1) : ''
}

function getTableData() {
	const currYear = localStorage.getItem("year");
	const currMon = localStorage.getItem("month");
	// $.get(
	// 	`${API_URL}getdata?status=2&chartPage=604&chartNo=4`, {},
	// 	function(res) {
	// 		console.log(res)
	// 		let re = eval("(" + res + ")");
	// 		console.log(re)
	// 		// getQSCost(re.items[0].charts[0].data)
	// 	}
	// )
	$.get(
		`${API_URL}getdata?budgetYear=${currYear}&budgetMonth=${currMon}&status=2&chartPage=604&chartNo=4&username=${getUsername}`, {},
		function(res) {
			console.log(res)
			let re = eval("(" + res + ")");
			console.log(re)
			let data = re.items[0].charts[0].data;
			let id = 0;
			let parentData = [];
			for (let i = 0; i < data.length; i++) {
				if (data[i].key02 == '') {
					++id;
					data[i].id = id;
					data[i].pid = 0;
					parentData.push(data[i])
				}
				// if(data[i].key03 == ''){
				//  data[i].key03 = '-'
				// }
			}
			console.log(parentData)
			for (let i = 0; i < parentData.length; i++) {
				for (let j = 0; j < data.length; j++) {
					if (parentData[i].key01 == data[j].key02) {
						data[j].pid = parentData[i].id
					}
				}
			}
			console.log(data)
			getIndirect(data)
			setTimeout(function() {
				$(".bar").peity("bar", {
					fill: ['#165DFF']
				})
			}, 1000)
		}
	)
}

function getIndirect(data) {
	let $table = $('#indirect');
	$table.bootstrapTable('destroy');
	$('.container .box.box5').width($('.container .content .wrapper').width() - 52)
	$table.bootstrapTable({
		// url: 'http://127.0.0.1:8848/cl/assets/plugins/treegrid.json',
		data: data,
		height: 400,
		idField: 'id',
		showColumns: false,
		fixedColumns: true,
		fixedRightNumber: 2,
		showFooter: true,
		// footerStyle: footerStyle,
		columns: [
			[{
					field: 'key01',
					title: '项目',
					rowspan: 2,
					valign: 'middle',
					width: 150,
					formatter: blodText,
					footerFormatter: total
				},
				{
					title: '一事业部',
					colspan: 2,
					width: 300,
					align: 'center',
				},
				{
					title: '二事业部',
					colspan: 2,
					width: 300,
					align: 'center',
				},
				{
					title: '三事业部',
					colspan: 2,
					width: 300,
					align: 'center',
				},
				{
					title: '四事业部',
					colspan: 2,
					width: 300,
					align: 'center',
				},
				{
					title: '其他',
					colspan: 2,
					width: 300,
					align: 'center',
				},
				{
					field: 'key13',
					title: '本月发生合计',
					rowspan: 2,
					sortable: true,
					valign: 'middle',
					width: 150,
					align: 'right',
					formatter: chartText,
					footerFormatter: sum
				},
				{
					field: 'key14',
					title: '本年累计合计',
					rowspan: 2,
					sortable: true,
					valign: 'middle',
					width: 150,
					align: 'right',
					formatter: chartText,
					footerFormatter: sum
				}
			],
			[{
					field: 'key03',
					title: '本月发生',
					sortable: true,
					align: 'right',
					footerFormatter: sum,
					formatter:wyText,
				},
				{
					field: 'key04',
					title: '本年累计',
					sortable: true,
					align: 'right',
					formatter:wyText,
					footerFormatter: sum
				},
				{
					field: 'key05',
					title: '本月发生',
					sortable: true,
					align: 'right',
					footerFormatter: sum,
					formatter:wyText,
				},
				{
					field: 'key06',
					title: '本年累计',
					sortable: true,
					align: 'right',
					footerFormatter: sum,
					formatter:wyText,
				},
				{
					field: 'key07',
					title: '本月发生',
					sortable: true,
					align: 'right',
					footerFormatter: sum,
					formatter:wyText,
				},
				{
					field: 'key08',
					title: '本年累计',
					sortable: true,
					align: 'right',
					footerFormatter: sum,
					formatter:wyText,
				},
				{
					field: 'key09',
					title: '本月发生',
					sortable: true,
					align: 'right',
					footerFormatter: sum,
					formatter:wyText,
				},
				{
					field: 'key10',
					title: '本年累计',
					sortable: true,
					align: 'right',
					footerFormatter: sum,
					formatter:wyText,
				},
				{
					field: 'key11',
					title: '本月发生',
					sortable: true,
					align: 'right',
					footerFormatter: sum,
					formatter:wyText,
				},
				{
					field: 'key12',
					title: '本年累计',
					sortable: true,
					align: 'right',
					footerFormatter: sum,
					formatter:wyText,
				}

			]

		],
		treeShowField: 'key01',
		parentIdField: 'pid',
		onPostBody: function() {
			var columns = $table.bootstrapTable('getOptions').columns
			if (columns && columns[0][1].visible) {
				$table.treegrid({
					treeColumn: 0,
					onChange: function() {
						$table.bootstrapTable('resetView')
					}
				})
			}
		}
	})


}

function total() {
	return '合计'
}

// function footerStyle(column) {
// 	return {
// 		name: {
// 			css: {
// 				'width': '150px'
// 			}
// 		}
// 	} [column.field]
// }

function sum(data) {
    var field = this.field;
    return  blodText(FormatWYCash(data.map(function (row) {
      return +row[field]
    }).reduce(function (sum, i) {
      return sum + i
    }, 0) + ''))
}
function sumChart(data){
	var field = this.field;
	// return `<div class="chartText">
	// <span class="bar">5,3,9,6,5,9,7,3,5,2</span>
	// ${blodText(FormatWYCash(data.map(function (row) {
 //      return +row[field]
 //    }).reduce(function (sum, i) {
 //      return sum + i
 //    }, 0)))}
	// </div>`
}

function wyText(value){
	return `<p>${FormatWYCash(value)}</p>`
}
function blodText(value) {
	return `<h6>${value}</h6>`
}

function chartText(value) {
	console.log(this.field)
	// return `<div class="chartText">
	// <span class="bar">5,3,9,6,5,9,7,3,5,2</span>
	// ${blodText(FormatWYCash(value))}
	// </div>`
	return `${blodText(FormatWYCash(value))}`
}
